package com.attendance.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.entity.ApplyLeave;
import com.attendance.entity.dto.ApplyLeaveDTO;
import com.attendance.service.IApplyLeaveService;
import com.common.util.Examine;
import com.common.util.ExtjsAjaxResponse;
import com.common.util.ExtjsPageable;
import com.staff.entity.Staff;
import com.staff.service.IStaffService;

@Controller
@RequestMapping("/applyLeave")
public class ApplyLeaveController {
	@Autowired
	private IApplyLeaveService applyLeaveService;
	@Autowired
	private IStaffService staffService;
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<ApplyLeaveDTO> findPage(String staffName,String staffNum,ExtjsPageable pageable){
		try {
			return applyLeaveService.findPage(staffName, staffNum, pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findPersonalApply")
	public @ResponseBody Page<ApplyLeaveDTO> findPersonalApply(HttpSession session,ExtjsPageable pageable){
		try {
			Long staffId = (Long) session.getAttribute("staffId");
			Staff staff = staffService.findOne(staffId);
			if(staff == null)return null;
			return applyLeaveService.findPersonalApply(staff.getId(), pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	//未审核记录
	@RequestMapping("/findUnauditedApply")
	public @ResponseBody Page<ApplyLeaveDTO> findUnauditedApply(HttpSession session,ExtjsPageable pageable){
		try {
			if(session == null) return null;
			Long staffId = (Long) session.getAttribute("staffId");
			Staff staff = staffService.findOne(staffId);
			if(staff == null)return null;
			return applyLeaveService.findUnauditedApply(staff.getId(), pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtjsAjaxResponse saveOrUpdate(ApplyLeave applyLeave,HttpSession session) {
		try {
			if(session == null) return new ExtjsAjaxResponse(false,"操作失败！");
			Long staffId = (Long) session.getAttribute("staffId");
			Staff staff = staffService.findOne(staffId);
			if(staff == null) {
				System.out.println("staffweikongID:"+staffId);
				return new ExtjsAjaxResponse(false,"操作失败！");
				
			}
			if(applyLeave.getId()!=null) {
				if(!applyLeaveService.isLauncher(staff.getId(), applyLeave.getId())) {
					System.out.println("失败1");
					return new ExtjsAjaxResponse(false,"操作失败！");
				}else {
					//更新
					ApplyLeave al = applyLeaveService.findOne(applyLeave.getId());
					if(al.getStatus()==Examine.Pass)
						return new ExtjsAjaxResponse(false,"该审核状态下不可编辑！");
					applyLeaveService.saveOrUpdate(applyLeave, staff);
					return new ExtjsAjaxResponse(true,"操作成功！");
				}
			}else {
				System.out.println("新增记录");
				applyLeaveService.saveOrUpdate(applyLeave,staff);
				return new ExtjsAjaxResponse(true,"操作成功！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/toExamine")//审核申请
	public @ResponseBody ExtjsAjaxResponse toExamine(ApplyLeave applyLeave,Integer flag,HttpSession session) {
		try {
			//0Unaudited,1Pass,2NotPass,3Retracted;
			Long staffId = (Long) session.getAttribute("staffId");
			Staff auditor = staffService.findOne(staffId);
			if((flag == 1||flag == 2)&&applyLeave!=null&&auditor!=null) {
				Examine exam = null;
				if(flag == 1)exam = Examine.Pass;
				if(flag == 2)exam = Examine.NotPass;
				applyLeaveService.toExamine(applyLeave, exam, auditor.getId());
				return new ExtjsAjaxResponse(true,"操作成功！");
			}
			return new ExtjsAjaxResponse(false,"操作失败！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/callBackApply")
	public @ResponseBody ExtjsAjaxResponse callBackApply(Long id,HttpSession session) {
		try {
			Long staffId = (Long) session.getAttribute("staffId");
			Staff staff = applyLeaveService.getStaff(id);
			if(staff==null||staff.getId() != staffId)return new ExtjsAjaxResponse(false,"操作失败！");
			ApplyLeave applyLeave = applyLeaveService.findOne(id);
			if(applyLeave.getStatus()!=Examine.Unaudited&&applyLeave.getStatus()!=Examine.NotPass)return new ExtjsAjaxResponse(false,"当前审核状态不可撤回！");
			applyLeaveService.callBack(id, staffId);
			return new ExtjsAjaxResponse(true,"撤回成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
		
	}
}
